package com.ylfstu.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {

        Collection collection = new ArrayList();
        collection.add(new Book("三国演义", "罗贯中", 50.98));
        collection.add(new Book("水浒传", "施耐庵", 32.1));
        collection.add(new Book("西游记", "吴承恩", 20.1));

        System.out.println(collection);

        //遍历collection集合
        //1.先得到collection对应的迭代器
        Iterator iterator = collection.iterator();
        //2.用while遍历
        while (iterator.hasNext()) {
            //返回下一个元素，类型是Object
            Object obj = iterator.next();
            System.out.println(obj);
        }

        //快捷键 快速生成while ==> itit     显示所有快捷键的快捷键 ctrl + j
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }
        //3.当退出while循环后，这时iterator迭代器指向最后的元素
//        iterator.next();//抛出NoSuchElementsException
        //4.如果希望再次遍历，需要重置我们的迭代器
        iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println();
        }
        

    }
}

class Book {
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
